/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.newlink.building.activities.main.settings.fragments

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.newlink.building.R
import com.newlink.building.activities.main.settings.viewmodels.Module_Phone_NetworkSettingsViewModel
import com.newlink.building.databinding.SettingsNetworkFragmentBinding
import com.newlink.building.utils.Module_Phone_Event
import com.newlink.building.utils.Module_Phone_ToastUtils
import com.newlink.building.widget.Module_Phone_CustomAlertDialog
import org.linphone.core.tools.Log

class Module_Phone_NetworkSettingsFragment : Module_Phone_GenericSettingFragment<SettingsNetworkFragmentBinding>() {
    private lateinit var viewModel: Module_Phone_NetworkSettingsViewModel

    override fun getLayoutId(): Int = R.layout.settings_network_fragment

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.lifecycleOwner = viewLifecycleOwner
        binding.sharedMainViewModel = sharedViewModel

        viewModel = ViewModelProvider(this)[Module_Phone_NetworkSettingsViewModel::class.java]
        binding.viewModel = viewModel

        binding.setBackClickListener { goBack() }

        binding.setSaveClickListener {

            if (viewModel.checkPreferenceChanged()) {
                viewModel.savePreferenceChanged()
                Module_Phone_ToastUtils.showTextToast(context!!, "已经保存修改")
            } else {
                Module_Phone_ToastUtils.showTextToast(context!!, "没有修改的内容")
            }
        }
    }

    override fun goBack() {

        Log.e("jake->", "CallSettingfragment onGoback")
        if (viewModel.checkPreferenceChanged()) {
            showPreferenciesChangedDialog(context!!)
            return
        }
        exitPage()
    }

    private fun showPreferenciesChangedDialog(context: Context) {

        val dialog = Module_Phone_CustomAlertDialog(context).apply {
            setTitle(R.string.settings_save_warning_title)
            setContentText(resources.getString(R.string.settings_save_warning_content))
            setPositiveButton(resources.getString(R.string.save_dialog_custom)) {
                viewModel.savePreferenceChanged()
                exitPage()
                this.dismiss()
            }
            setNegativeButton(resources.getString(R.string.cancel_dialog_custom)) {
                exitPage()
                this.dismiss()
            }
        }
        dialog.show()
    }

    private fun exitPage() {
        sharedViewModel.hideNavigationBar.value = false
        sharedViewModel.jumpToSettingsPage.value = Module_Phone_Event(true)
    }
}
